package com.jxau.mall.goods.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jxau.mall.goods.entity.Category;
import com.jxau.mall.goods.entity.SkuInfo;
import com.jxau.mall.goods.mapper.CategoryMapper;
import com.jxau.mall.goods.mapper.SkuInfoMapper;
import com.jxau.mall.goods.model.Product;
import com.jxau.mall.goods.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;


/**
 * @program: shop
 * @description: IProductService实现类，用于写具体的实现方法
 * @author: guojiani
 * @create: 2025-04-20 21:41
 **/
@Service
public class ProductServiceImpl implements IProductService {

   @Autowired
   private SkuInfoMapper skuInfoMapper;

   @Autowired
   private CategoryMapper categoryMapper;


    @Override
    public IPage page(Product product) {
        //创建条件构造器
        LambdaQueryChainWrapper<SkuInfo> skuWrapper =
                new LambdaQueryChainWrapper<>(skuInfoMapper);
        /*
         * 按商品类别查询
         * 如果三级类别不为空，按三级类别查询
         * 否则，如果二级类别不为空，按二级类别查询
         * 否则，如果一级类别不为空，按一级类别查询
         */
        //如果三级类别不为空，按三级类别查询
        if (product.getCategoryId() != null) {
            skuWrapper.eq(SkuInfo::getCategoryId, product.getCategoryId());
            //相当于SkuInfo::getCategoryId等于product.getCategoryId()
        } //否则，如果二级类别不为空，按二级类别查询
        else if (product.getTwoCategoryId() != null) {
            //1构造查询条件
            //1.1查询三级类别
            LambdaQueryChainWrapper<Category> categoryWrapper =
                    new LambdaQueryChainWrapper<>(categoryMapper);
            List<Category> list = categoryWrapper.eq(Category::getParentId, product.getTwoCategoryId())
                    .select(Category::getId)
                    .list();
            //1.2提取ID(将以上list集合中的id（该id为三级id）提取出来单独放到一个集合中)
            List<Integer> ids =
                    list.stream().map(Category::getId).collect(Collectors.toList());
            //2.构建商品查询条件
            skuWrapper.in(SkuInfo::getCategoryId, ids);
        }
        //否则，如果一级类别不为空，按一级类别查询
        else if (product.getOneCategoryId() != null) {
            // 1 查询一级类别下的所有二级类别
            // 分类查询条件构建器
            LambdaQueryChainWrapper<Category> oneWrapper = new
                    LambdaQueryChainWrapper<>(categoryMapper);
            List<Category> ones = oneWrapper.eq(Category::getParentId,product.getOneCategoryId())
                    .select(Category::getId).list();
            // 提取类别 ID
            List<Integer> oneIds =
                    ones.stream().map(Category::getId).collect(Collectors.toList());
            // 2 查询二级类别下的所有三级类别
            // 重建查询条件构造器
            LambdaQueryChainWrapper<Category> twoWrapper = new
                    LambdaQueryChainWrapper<>(categoryMapper);
            List<Category> list = twoWrapper.in(Category::getParentId, oneIds)
                    .select(Category::getId).list();
            // 提取类别 ID
            List<Integer> ids =
                    list.stream().map(Category::getId).collect(Collectors.toList());
            // 3 构建查询条件
            skuWrapper.in(SkuInfo::getCategoryId,ids);
        }
        //按品牌查询
        if(product.getBrandId() !=null){
            skuWrapper.eq(SkuInfo::getBrandId, product.getBrandId());
        }
        //按品名查询
        if(product.getBrandName() !=null){
            skuWrapper.like(SkuInfo::getSkuName, product.getBrandName());
        }

        //选择列，也就是选择需要返回的列名
        skuWrapper.select(SkuInfo::getId,SkuInfo::getSkuName,SkuInfo::getPrice,SkuInfo::getSkuDefaultImg);

        //分页
        Page page = new Page();
        if (product.getCurrent() !=null && product.getSize()!=0) {
           // page = new Page(product.getCurrent(), product.getSize());
            // 或者用上面那种方法
            page.setCurrent(product.getCurrent());
            page.setSize(product.getSize());
        }
        return skuWrapper.page(page);
    }
}
